﻿using GptChatBatch.WinUI.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace GptChatBatch.WinUI.Services
{
    public class QuestionTplService
    {
        private XmlDocument _xmlDoc;
        private string _xmlFilePath;

        public QuestionTplService()
        {
            _xmlFilePath = @"CfgFiles/question_tpls.xml";
            CreateXmlFile(_xmlFilePath);
            _xmlDoc=new XmlDocument();
            _xmlDoc.Load(_xmlFilePath);
        }
        // 创建XML文件
        private bool CreateXmlFile(string filePath) 
        {
            if (File.Exists(filePath)) return true;
            try
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                XmlElement root = doc.CreateElement("Root");
                doc.AppendChild(root);
                doc.Save(filePath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        //查询
        public List<QuestionTpl> GetList()
        {
            List<QuestionTpl> questionTpls=new List<QuestionTpl>();
            XmlNodeList nodes = _xmlDoc.SelectNodes("Root/QuestionTpl");
            foreach( XmlNode node in nodes)
            {
                QuestionTpl tpl = new QuestionTpl();
                PropertyInfo[] properties = tpl.GetType().GetProperties();
                foreach (PropertyInfo propertyInfo in properties)
                {
                    XmlNode ele = node.SelectSingleNode(propertyInfo.Name);
                    if (ele != null)
                    {
                        propertyInfo.SetValue(tpl, ele.InnerText.Trim());
                    }                    
                }
                questionTpls.Add(tpl);

            }
            return questionTpls;
  
        }

        public void Modify(QuestionTpl questionTpl)
        {
            XmlNodeList nodeList = _xmlDoc.SelectNodes("Root/QuestionTpl/TplName");
            XmlNode tplNode=null;
            foreach (XmlNode node in nodeList)
            {
                if (node.InnerText.Trim() == questionTpl.TplName)
                {
                    tplNode=node.ParentNode;
                    break;
                }
            }
            if (tplNode == null) return;
            XmlElement eleQuestionTpl = _xmlDoc.CreateElement("QuestionTpl");
            PropertyInfo[] properties = questionTpl.GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                XmlElement ele = _xmlDoc.CreateElement(propertyInfo.Name);
                ele.InnerText = propertyInfo.GetValue(questionTpl)?.ToString();
                eleQuestionTpl.AppendChild(ele);
            }
            tplNode.ParentNode.InsertBefore(eleQuestionTpl,tplNode);
            tplNode.ParentNode.RemoveChild(tplNode);
            _xmlDoc.Save(_xmlFilePath);
        }

        public XmlElement Add(QuestionTpl questionTpl)
        {
          
            XmlNode root = _xmlDoc.SelectSingleNode("Root");            
            XmlElement eleQuestionTpl = _xmlDoc.CreateElement("QuestionTpl");
            PropertyInfo[] properties = questionTpl.GetType().GetProperties();
            foreach (PropertyInfo propertyInfo in properties)
            {
                XmlElement ele=_xmlDoc.CreateElement(propertyInfo.Name);
                ele.InnerText= propertyInfo.GetValue(questionTpl)?.ToString();
                eleQuestionTpl.AppendChild(ele);
            }
            root.AppendChild(eleQuestionTpl);
            _xmlDoc.Save(_xmlFilePath);
            return eleQuestionTpl;
        }

        //删除
        public void Delete(string tplName)
        {
            XmlNodeList nodeList = _xmlDoc.SelectNodes("Root/QuestionTpl/TplName");
            XmlNode tplNode = null;
            foreach (XmlNode node in nodeList)
            {
                if (node.InnerText.Trim() == tplName)
                {
                    tplNode = node.ParentNode;
                    break;
                }
            }
            if (tplNode == null) return;

            tplNode.ParentNode.RemoveChild(tplNode);
            _xmlDoc.Save(_xmlFilePath);
        }

        //修改
        public void Modify(string xmlNodePath, string nodeName, string innerText, Dictionary<string, string> attributes = null)
        {
            XmlNodeList nodeList = _xmlDoc.SelectSingleNode(xmlNodePath).ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                if (xn.Name == nodeName)
                {
                    xn.InnerText = innerText;
                    if (attributes != null)
                    {
                        xn.Attributes.RemoveAll();
                        foreach (var attribute in attributes)
                        {
                            var attr = _xmlDoc.CreateAttribute(attribute.Key);
                            attr.Value = attribute.Value;
                            xn.Attributes.Append(attr);
                        }
                    }
                    break;
                }
            }
            _xmlDoc.Save(_xmlFilePath);
        }

       

    }
}
